package com.liukai.novel_rack.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_rack.R;
import com.liukai.novel_rack.search_interface.OnSearchItemListener;

import java.util.ArrayList;
import java.util.List;

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.SearchResultViewHolder> {

    private List<NovelInfo> mNovelInfoList = new ArrayList<>();
    private Context mContext;

    public SearchResultAdapter(List<NovelInfo> novelInfoList, Context context) {
        this.mNovelInfoList = novelInfoList;
        this.mContext = context;
    }

    @NonNull
    @Override
    public SearchResultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SearchResultViewHolder(View.inflate(parent.getContext(), R.layout.item_search_result, null));
    }

    @Override
    public void onBindViewHolder(@NonNull SearchResultViewHolder holder, int position) {
        final NovelInfo novelInfo = mNovelInfoList.get(position);
        holder.mNovelName.setText(novelInfo.getNovelName());
        holder.mNovelDesc.setText(novelInfo.getNovelDesc());
        holder.mNovelAuthor.setText(novelInfo.getNovelAuthor());
//        Glide.with(mContext).load(novelInfo.getNovelImg()).into(holder.mNovelImg);
        Glide.with(mContext)
                .load(Constans.IMAGE_BASE_URL + novelInfo.getNovelImg())
                .into(holder.mNovelImg);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnSearchItemListener.onItemClick(novelInfo);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mNovelInfoList.size();
    }

    class SearchResultViewHolder extends RecyclerView.ViewHolder {

        public TextView mNovelName;
        public TextView mNovelDesc;
        public TextView mNovelAuthor;
        private final ImageView mNovelImg;

        public SearchResultViewHolder(@NonNull View itemView) {
            super(itemView);
            mNovelName = itemView.findViewById(R.id.novel_name);
            mNovelDesc = itemView.findViewById(R.id.desc);
            mNovelAuthor = itemView.findViewById(R.id.author_name);
            mNovelImg = itemView.findViewById(R.id.novel_img);
        }
    }

    public void setNovelInfoList(List<NovelInfo> novelInfoList) {
        mNovelInfoList.clear();
        mNovelInfoList.addAll(novelInfoList);
        notifyDataSetChanged();
    }

    private OnSearchItemListener mOnSearchItemListener;

    public void setOnSearchItemListener(OnSearchItemListener onSearchItemListener) {
        mOnSearchItemListener = onSearchItemListener;
    }
}
